#ifndef DOUBAN_DATA_CONTACTS_H
#define DOUBAN_DATA_CONTACTS_H

#include "DoubanUser.h"
#include "douban_global.h"
#include <QXmlStreamReader>
#include <QNetworkReply>
#include <QTimer>
#include <QHash>

namespace Douban
{
	class DoubanContext;
	class Contacts : public QObject
	{
		Q_OBJECT
		public:
			DoubanUser addUser(DoubanId id, QString name = QString::null, QString selfLink = QString::null);
			DoubanUser parseUser(QXmlStreamReader*,QStringRef tagName);

			const DoubanUser& mySelf() const { return m_myself; }


			void getSelfInfo();
			Contacts(DoubanContext* c);
			~Contacts();
		signals:
			void getSelfInfoFinished(bool);
		private:
			DoubanContext*				m_context;
			DoubanUser					m_myself;
			QHash<DoubanId,DoubanUser>	m_users;
			QXmlStreamReader				m_xmlParser;
			QVector<DoubanUser>			m_pendingAcquiredUsers;
			QVector<DoubanUser>			m_pendingUpdateImageUsers;

			QTimer cleanupTimer;

			QNetworkReply* r_getUserInfoReply;
			QNetworkReply* r_getUserImgReply;

			void acquireUserInfo();
			void addToUpdateImageQueue(const DoubanUser&);
			void updateNextUserImage();
		private slots:
			void onGetSelfInfo();
			void onGetUserInfo();
			void onGetUserImage();

			void cleanContact();
	};
}

#endif /* DOUBAN_DATA_CONTACTS_H */
